home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
MACSHELL
/
MS1
/
SHELL_SO
/
SHELLVAR.C
< prev
next >
Wrap
Text File
|
1992-12-02
|
10KB
|
374 lines
/*
* MacShell Source File
*
* Copyright (c) 1989, 1990, 1991, 1992 Suick Bay Technologies. All rights reserved.
*
*
* RESTRICTIONS ON MacShell program and source code.
*
* Ñ╩MacShell¬ is a product of Suick Bay Technologies and is provided for
* restricted use by the owner of the CDROM "Disk to the future II".
*
* Ñ╩No permission is granted for any commercial use without the written
* consent of the Suick Bay Technologies.
*
* Ñ╩No permission is granted for any redistribution of any kind use without
* the written consent of the Suick Bay Technologies.
*
* Ñ╩Permission is granted to use this for any personal noncommercial use.
*
* Ñ╩You may not distribute source or executable code at all, nor may you
* distribute it with or within a commercial product without the written
* consent of the Suick Bay Technologies. Please send modifications to
* the author for inclusion in updates to the program. Thanks.
*
*
* MacShell¬ IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
* WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
* PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
*
* SUICK BAY TECHNOLOGIES SHALL HAVE NO LIABILITY WITH RESPECT TO THE
* INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY MACSHELL
* OR ANY PART THEREOF.
*
* In no event will Suick Bay Technologies be liable for any lost revenue
* or profits or other special, indirect and consequential damages, even if
* Suick Bay Technologies has been advised of the possibility of such damages.
*
* Suick Bay Technologies can be reached at:
*
* 8768 Cottonwood lane
* Maple Grove, MN 55369
* Voice: (612) 425-7025
* AppleLink: D5233
*
*
* No parts of this software may be reproduced or stored in a
* retrieval system or transmitted in any form, or any means,
* electronic, mechanical, photocopying, recording or otherwise,
* without the prior written permission of Suick Bay Technologies.
*
* Spread the word and not the disk.
*
* SPK 030790 : Locked ch in ShellGetVar
* SPK 012290 : Initial
*/
#include "System.h"
#include "Global.h"
#include "Mac.h"
#include "Proc.h"
#include "Shell.h"
#include "Path.h"
#include "Global.h"
#include <OSUtil.h>
/*******************************************************************
*
*******************************************************************/
Boolean ShellSetVar( WHandle ShellWh, char *varName, char *value )
{
ShellWindRec **MyShell;
char buf[ MAXVALUESIZE+8 ];
int16 len;
MyShell = (ShellWindRec **) (**(ShellWh)).thing;
len = strlen( value ); /* clip the max size to our output buffer */
if( len > MAXVALUESIZE )
value[ MAXVALUESIZE ] = '\0';
sprintf( buf, "%d_ _%s", (**MyShell).shellID, varName );
RemShellString( buf );
AddShellString( buf, value );
return( TRUE );
}
void ShellDelVar( WHandle ShellWh, char *varName )
{
ShellWindRec **MyShell;
char buf[ 256 ];
MyShell = (ShellWindRec **) (**(ShellWh)).thing;
sprintf( buf, "%d_ _%s", (**MyShell).shellID, varName );
RemShellString( buf );
}
/*******************************************************************/
char tempValue[ MAXVALUESIZE ];
#define USERNAMEID -16096
char *ShellGetVar( WHandle ShellWh, char *varName )
{
int16 i, max;
ShellWindRec **MyShell;
MyShell = (ShellWindRec **) (**(ShellWh)).thing;
/*
* Special Mac variables that are read only
*
* BOOT volume name of the boot disk
* COMMAND name of the last command executed
* SHELL full pathname of the Shell
* SYSTEMFOLDER full pathname of the system folder
* USER the current user name (initialized to the ╥Chooser╙ name)
* TIME the current time
* DATE the current date
* FREEMEM the amount of free memory
*/
*tempValue = '\0';
if( strcmp( SHELL_BOOT, varName ) == 0 )
strcpy( tempValue, bootVol );
else if( strcmp( SHELL_SHELL, varName ) == 0 )
GetCurrPath( tempValue );
else if( strcmp( SHELL_SYSTEMFOLDER, varName ) == 0 )
GetPathFrom( bootSysRec.sysVRefNum, 0L, tempValue );
else if( strcmp( SHELL_USER, varName ) == 0 )
{
Handle ph;
ph = GetResource( 'STR ', USERNAMEID );
if( ph )
{
char *str = *ph;
PtoCstr( str );
strcpy( tempValue, str );
CtoPstr( str );
}
}
else if( strcmp( SHELL_TIME, varName ) == 0 )
GetShellTime( tempValue );
else if( strcmp( SHELL_DATE, varName ) == 0 )
GetShellDate( tempValue );
else if( strcmp( SHELL_FREEMEM, varName ) == 0 )
{
int32 free;
free = FreeMem();
sprintf( tempValue, "%ld", free );
}
else
{
char **ch;
sprintf( tempValue, "%d_ _%s", (**MyShell).shellID, varName );
ch = GetShellString( tempValue );
if( ch )
{
HLock( ch );
CopyStr( *ch, tempValue );
PtoCstr( tempValue );
HUnlock( ch );
}
else
*tempValue = '\0';
}
return( tempValue );
}
/*******************************************************************/
extern int16 appResFile;
void ShellDumpVar( WHandle ShellWh, int16 ProcID )
{
ShellWindRec **MyShell;
int16 i, numChars, id;
Handle h;
int32 num;
char strnum[ 16 ], *cp, *sp;
char name[ 256 ];
MyShell = (ShellWindRec **) (**(ShellWh)).thing;
UseResFile( appResFile );
i = Count1Resources( STRRES );
while( i )
{
h = GetIndResource( STRRES, i );
GetResInfo( h, &id, &num, name );
cp = name;
numChars = *cp++;
sp = strnum;
while( *cp != '_' && --numChars ) /* copy the shell id */
*sp++ = *cp++;
if( numChars )
{
*sp = '\0'; cp++; numChars--;
CtoPstr( strnum );
StringToNum( strnum, &num );
if( num == (int32) (**MyShell).shellID && *cp == ' ' )
{
sp = name;
while( *cp++ != '_' )
numChars--;
while( --numChars )
*sp++ = *cp++;
*sp = '\0';
CopyStr( *h, tempValue );
PtoCstr( tempValue );
procPrintf( ShellWh, ProcID, "%s=%s\n", name, tempValue );
}
}
i--;
}
procPrintf( ShellWh, ProcID, "%s=%s\n", SHELL_BOOT, ShellGetVar( ShellWh, SHELL_BOOT ));
procPrintf( ShellWh, ProcID, "%s=%s\n", SHELL_SHELL, ShellGetVar( ShellWh, SHELL_SHELL ));
procPrintf( ShellWh, ProcID, "%s=%s\n", SHELL_SYSTEMFOLDER, ShellGetVar( ShellWh, SHELL_SYSTEMFOLDER ));
procPrintf( ShellWh, ProcID, "%s=%s\n", SHELL_USER, ShellGetVar( ShellWh, SHELL_USER ));
procPrintf( ShellWh, ProcID, "%s=%s\n", SHELL_TIME, ShellGetVar( ShellWh, SHELL_TIME ));
procPrintf( ShellWh, ProcID, "%s=%s\n", SHELL_DATE, ShellGetVar( ShellWh, SHELL_DATE ));
procPrintf( ShellWh, ProcID, "%s=%s\n", SHELL_FREEMEM, ShellGetVar( ShellWh, SHELL_FREEMEM ));
}
/*******************************************************************/
void InitShellVars( WHandle ShellWh )
{
ShellWindRec **MyShell;
MyShell = (ShellWindRec **) (**(ShellWh)).thing;
/*
* Standard UNIX variables
*/
ShellSetVar( ShellWh, SHELL_QUES, "0" );
ShellSetVar( ShellWh, SHELL_ZERO, "Shell" );
ShellSetVar( ShellWh, SHELL_PND, "0" );
ShellSetVar( ShellWh, SHELL_DOLLAR, "3" );
ShellSetVar( ShellWh, SHELL_EXCL, "3" );
ShellSetVar( ShellWh, SHELL_DASH, "-" );
ShellSetVar( ShellWh, SHELL_HOME, "." );
ShellSetVar( ShellWh, SHELL_CDPATH, "/" );
ShellSetVar( ShellWh, SHELL_PATH, ".;/" );
ShellSetVar( ShellWh, SHELL_PS1, "$" );
ShellSetVar( ShellWh, SHELL_PS2, ">" );
ShellSetVar( ShellWh, SHELL_IFS, " \n\t" );
ShellSetVar( ShellWh, SHELL_COMMAND, "SH" );
}
/*******************************************************************/
Boolean DoSET( int16 ProcToken, WHandle ShellWh, int16 ProcID, char *string )
{
int16 i, index;
char var[ 256 ],
value[ 256 ],
*cp, *vp;
ShellWindRec **MyShell = (ShellWindRec **) (**ShellWh).thing;
switch( ProcToken )
{
case PROC_INIT :
(**MyShell).Proc[ ProcID ].flags = TRUE;
break;
case PROC_TERM :
case PROC_BREAK :
SendOutToken( ShellWh, ProcID, PROC_BREAK );
/* Turn ourself off */
(**MyShell).Proc[ ProcID ].ProcActive = FALSE;
break;
case PROC_STDIN :
if( (**MyShell).Proc[ ProcID ].flags )
{
(**MyShell).Proc[ ProcID ].flags = FALSE;
if( (**MyShell).Proc[ ProcID ].argc == 1 )
ShellDumpVar( ShellWh, ProcID );
for( i = 1; i < (**MyShell).Proc[ ProcID ].argc; i++ )
{
index = (**MyShell).Proc[ ProcID ].argv[ i ];
cp = &((**MyShell).Proc[ ProcID ].cmdline[ index ]);
vp = var;
while( *vp = *cp )
{
if( *vp == '=' )
{
*vp = '\0';
vp = value;
cp++;
}
else
{
vp++; cp++;
}
}
ShellSetVar( ShellWh, var, value );
}
}
/* Tell the shell that we're done */
SendOutToken( ShellWh, ProcID, PROC_BREAK );
/* Turn ourself off */
(**MyShell).Proc[ ProcID ].ProcActive = FALSE;
return( FALSE );
}
}
/*******************************************************************/
Boolean DoUNSET( int16 ProcToken, WHandle ShellWh, int16 ProcID, char *string )
{
int16 i, argc;
char argument[ 256 ];
ShellWindRec **MyShell = (ShellWindRec **) (**ShellWh).thing;
switch( ProcToken )
{
case PROC_INIT :
(**MyShell).Proc[ ProcID ].flags = TRUE;
break;
case PROC_TERM :
case PROC_BREAK :
SendOutToken( ShellWh, ProcID, PROC_BREAK );
/* Turn ourself off */
(**MyShell).Proc[ ProcID ].ProcActive = FALSE;
break;
case PROC_STDIN :
if( (**MyShell).Proc[ ProcID ].flags )
{
(**MyShell).Proc[ ProcID ].flags = FALSE;
argc = (**MyShell).Proc[ ProcID ].argc;
for( i = 1; i < argc; i++ )
{
GetArgv( ShellWh, ProcID, i, argument );
ShellDelVar( ShellWh, argument );
}
}
/* Tell the shell that we're done */
SendOutToken( ShellWh, ProcID, PROC_BREAK );
/* Turn ourself off */
(**MyShell).Proc[ ProcID ].ProcActive = FALSE;
return( FALSE );
}
}